package com.demo.webapp.kcfinder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.demo.webapp.kcfinder.context.ActionContext;
import com.demo.webapp.kcfinder.utils.ValueStack;

/**
 * KCFinder请求封装实例对象 该封装对象将支持JSTL访问。 进而引入MVEL表达式
 * 
 * @author ryuu.kk
 * 
 */
public class KCFinderRequestWrapper extends HttpServletRequestWrapper {

	public KCFinderRequestWrapper(HttpServletRequest request) {
		super(request);
	}

	/**
	 * 取得属性值 如果基础对象无法查询到,则使用MVEL容器查询
	 * 
	 * @return Object
	 */
	public Object getAttribute(String s) {

		if (s != null && s.startsWith("javax.servlet")) {
			// don't bother with the standard javax.servlet attributes, we can
			// short-circuit this
			return super.getAttribute(s);
		}
		ActionContext context = ActionContext.getContext();
		Object attribute = super.getAttribute(s);
		if (context != null) {
			if (attribute == null) {
				boolean alreadyIn = false;
				Boolean b = (Boolean) context.getLock("__requestWrapper.getAttribute");
				if (b != null) {
					alreadyIn = b.booleanValue();
				}

				if (!alreadyIn && s.indexOf("#") == -1) {
					try {
						// If not found, then try the ValueStack
						context.lock("__requestWrapper.getAttribute", Boolean.TRUE);
						ValueStack stack = context.getValueStack();
						attribute = stack.findValue(s);
						
					} finally {
						context.lock("__requestWrapper.getAttribute", Boolean.FALSE);
					}
				}
			}
		}
		return attribute;
	}
}
